import 'package:enterprise_v6_app/app/components/custom_app_bar.dart';
import 'package:enterprise_v6_app/app/components/custom_refresh.dart';
import 'package:enterprise_v6_app/app/components/special_operation/custom_title_chart_page.dart';
import 'package:enterprise_v6_app/app/core/core.dart';
import 'package:enterprise_v6_app/app/core/ext/ext.dart';
import 'package:enterprise_v6_app/app/data/entities/double_control/hidden_danger_investigation_ledger_entity.dart';
import 'package:enterprise_v6_app/app/modules/task/task_all/hazard_investigation/annual_hazard_plan/annual_hazard_plan_controller.dart';

///年度隐患排查计划
class AnnualHazardInvestigationPlanPage extends StatelessWidget {
  AnnualHazardInvestigationPlanPage({super.key});

  final controller = Get.put(AnnualHazardInvestigationPlanController());

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: CustomAppBar(
        titleText: '年度隐患排查计划',
      ),
      key: controller.riskRightScaffoldKey,
      body: Container(
        color: AppColors.bg_color_grey,
        padding: EdgeInsets.only(right: 14.w, left: 14.w),
        child: Column(
          children: [
            AppGaps.vGap12,
            Expanded(
                child: CustomEasyRefreshList<HiddenDangerInvestigationLedgerRecords>(
                  key: controller.globalKey,
                  dataLoad: controller.getPatrolScheme,
                  itemBuilder: (item, index) {
                    return _itemWidget(item);
                  },
                )),
          ],
        ),
      ), // 右侧筛选栏
    );
  }

  Widget _topTitle(String title, Color color, String? value, Color square) {
    return Row(
      crossAxisAlignment: CrossAxisAlignment.center,
      mainAxisAlignment: MainAxisAlignment.start,
      children: [
        Text(
          title,
          style: TextStyle(
            fontSize: 14.sp,
            color: color,
            height: 1.2,
            fontWeight: FontWeight.w400,
          ),
        ),
        AppGaps.hGap12,
        Text(
          value ?? '空',
          style: TextStyle(
            fontSize: 16.sp,
            color: square,
            fontWeight: FontWeight.w500,
          ),
        ),
      ],
    );
  }

  Widget _rowData(String name, String value, {MainAxisAlignment mainAxisAlignment = MainAxisAlignment.spaceBetween}) {
    return Row(
      mainAxisAlignment: mainAxisAlignment,
      children: [
        Text(
          name,
          style: TextStyle(
            color: const Color(0xFFA5A6B5),
            fontWeight: FontWeight.w400,
            fontSize: 12.sp,
          ),
        ),
        Expanded(
          child: Text(
            value,
            style: TextStyle(
              color: const Color(0xFF495061),
              fontSize: 12.sp,
              fontWeight: FontWeight.w400,
            ),
          ),
        )
      ],
    );
  }

  ///分割线
  Widget _divider() {
    return Container(
      width: 1.w,
      height: 24.w,
      decoration: const BoxDecoration(
          gradient: LinearGradient(begin: Alignment.topCenter, end: Alignment.bottomCenter, colors: [
            Color(0xFFFFFFFF),
            Color(0xFFFFFFFF),
          ])),
    );
  }

  /// 获取审批按钮
  Widget _getApprovalButton(HiddenDangerInvestigationLedgerRecords item) {
    return Row(
      mainAxisAlignment: MainAxisAlignment.spaceBetween,
      children: [
        Text(
          '操作:',
          style: TextStyle(
            fontSize: 13.sp,
            height: 1.2,
            fontWeight: FontWeight.w400,
            color: const Color(0xffA5A6B5),
          ),
        ),
        Container(
          padding: EdgeInsets.fromLTRB(10.w, 6.w, 6.w, 6.w),
          decoration: BoxDecoration(
            color: const Color(0xFFE5E7FF),
            borderRadius: BorderRadius.circular(4.w),
          ),
          child: Row(
            children: [
              Text(
                '审批',
                style: TextStyle(
                  fontSize: 13.sp,
                  height: 1.2,
                  fontWeight: FontWeight.w400,
                  color: const Color(0xff5C6DFF),
                ),
              ),
              AppGaps.hGap2,
              SvgPicture.asset(
                AppImages.icon_right_arrow,
              ),
            ],
          ),
        )
      ],
    );
  }

  /// 根据状态获取状态图标
  /// 审批状态 0：待发起 1：待审批 2：审批中 3：通过 4：驳回
  static Widget getStateView(int state) {
    String? stateText;
    Color? color;
    switch (state) {
      case 0:
        stateText = '待发起';
        color = const Color(0xFF0ED0D0);
        break;
      case 1:
        stateText = '待审批';
        color = const Color(0xFFFFA500);
        break;
      case 2:
        stateText = '审批中';
        color = const Color(0xFFB9B907);
        break;
      case 3:
        stateText = '通过';
        color = const Color(0xFF0FCE0F);
        break;
      case 4:
        stateText = '已驳回';
        color = const Color(0xFFBB0CBB);
        break;
    }
    return Container(
      alignment: Alignment.center,
      height: 18.w,
      width: 52.w,
      decoration: BoxDecoration(
        color: color ?? Colors.white,
        borderRadius: BorderRadius.only(
          topRight: Radius.circular(20.w),
          topLeft: Radius.circular(20.w),
          bottomRight: Radius.circular(20.w),
        ),
      ),
      child: Text(
        stateText ?? '',
        style: TextStyle(
          fontSize: 10.sp,
          fontWeight: FontWeight.w400,
          color: Colors.white,
        ),
      ),
    );
  }

  Widget _itemWidget(HiddenDangerInvestigationLedgerRecords item) {
    return  CustomTitleChartPage(
      title: item.planName ?? '',
      titleRightWidget: getStateView(item.checkType ?? -1),
      child: Column(
        crossAxisAlignment: CrossAxisAlignment.start,
        children: [
          _rowData('排查类型：', item.inspectionTypeName ?? '空'),
          AppGaps.vGap12,
          _rowData('排查部门：', '${item.inspectionDepartmentName ?? '空'}-${item.inspectionPersonnelName ?? '空'}'),
          AppGaps.vGap12,
          Container(
            padding: EdgeInsets.symmetric(vertical: 10.w),
            decoration: BoxDecoration(
              borderRadius: BorderRadius.circular(8),
              color: const Color(0x1A5C6DFF),
            ),
            child: Row(
              mainAxisAlignment: MainAxisAlignment.spaceEvenly,
              children: [
                Column(
                  children: [
                    _topTitle('点位', const Color(0xFF495061), '${item.numberOfPoints ?? '0'}', const Color(0xFF5C6DFF)),
                  ],
                ),
                _divider(),
                Column(
                  children: [
                    _topTitle('任务', const Color(0xFF495061), '${item.taskNum ?? '0'}', const Color(0xFF5C6DFF)),
                  ],
                ),
              ],
            ),
          ),
          Column(
            children: [
              AppGaps.vGap12,
              _getApprovalButton(item),
            ],
          )
        ],
      ),
    ).click(() {
      controller.onClickButtonPressed(item);
    });
  }
}
